/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
  <file> tok_remap.c </file>
  <brief>
    Reassign char-tok '{', '}' to non-ASCII(for example, 300, 301).
    This file is part of ULS, Unified Lexical Scheme.
  </brief>
  <author>
    Stanley J. Hong <babel96@gmail.com>, 2011.
  </author>
*/

#include "uls.h"

#include "sample_lex.h"
#include "uls.h"

#include <unistd.h>
#include <getopt.h>
#include <fcntl.h>
#include <time.h>

char *progname;
int  opt_verbose;

char *config_name;
char *input_file;

uls_lex_t *sample_lex;

static void usage(void)
{
	err_log("%s v1.0", progname);
	err_log("  The token number the 1-char-token(tok=='{', '}', '!', '@' ...) is by default itself(ACSII)");
	err_log("  '%s' demonstrates how the token numbers of those tokens can be remapped.", progname);
	err_log("");
	err_log(" Usage:");
	err_log("  %s [-c <config-file>] <inputfile-1> ...", progname);
	err_log("");
	err_log("  For example,");
	err_log("      %s input1.txt", progname);
	err_log("  A default config-file used, %s if you don't specifiy the config-file.", config_name);
	exit(1);
}

static int
parse_options(int argc, char* argv[])
{
	int   opt;

	while ((opt=getopt(argc, argv, "c:vh")) != -1) {
		switch (opt) {
		case 'c':
			config_name = optarg;
			break;

		case 'v':
			opt_verbose = 1;
			break;

		case 'h':
			usage();
			break;

		default:
			err_log("undefined option -%c", opt);
			return -1;
			break;
		}
	}

	return optind;
}

void
test_uls(char* fpath)
{
	int tok;

	if (uls_set_file(sample_lex, fpath, 0) < 0) {
		err_log(" file open error");
		return;
	}

	for ( ; ; ) {
		if ((tok=uls_gettok(sample_lex)) == TOK_EOI) break;

		if (tok == TOK_BEGIN || tok == TOK_END) {
			uls_printf("\t%3d  %s\n", tok, uls_lexeme_text(sample_lex));

		} else if (tok == TOK_LF) {
			uls_printf("\t%3d  \\n\n", TOK_LF);

		} else if (tok == TOK_TAB) {
			uls_printf("\t%3d  \\t\n", TOK_TAB);

		} else if (sample_lex->tok == TOK_MINUS) {
			uls_printf("\t%3d  MINUS\n");
		}
	}
}

int
main(int argc, char* argv[])
{
	int i0;

	progname = uls_filename(argv[0]);
	config_name = "sample.ulc";
	
	if ((i0=parse_options(argc, argv)) < 0 || i0 >= argc) {
		usage();
		return -1;
	}

	input_file = argv[i0];

	if ((sample_lex = uls_create(config_name)) == NULL) {
		err_log("can't init uls-lexer");
		return -1;
	}

	test_uls(input_file);

	uls_destroy(sample_lex);

	return 0;
}
